import  subprocess
# ping 主函数
def pingip(user_ip):
    try:
        # 使用 Popen 并设置 creationflags 参数
        process = subprocess.Popen(
            ['ping', user_ip],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            creationflags=subprocess.CREATE_NO_WINDOW  # 隐藏命令行窗口
        )

        # 获取输出和错误
        stdout, stderr = process.communicate(timeout=10)

        if process.returncode == 0:
            return stdout
        else:
            return f"Ping失败: {stderr}"

    except subprocess.TimeoutExpired:
        return "Ping超时"

    except Exception as e:
        return str(e)
